﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.DurableInstancing;
using System.Runtime.DurableInstancing;
using Common;
using System.Xml.Linq;

namespace OneGateWF
{
    public class ValidateTaskHost 
    {
        public void InvokeValidateTaskWF(ValidateTask task,CreateAdditionalInfo inf)
        {

            WorkflowApplication instance = CreateInstance(task);
            task.Id = instance.Id;            
            instance.Extensions.Add(inf);
            instance.Run();
        }

        public void OnIdle(WorkflowApplicationIdleEventArgs e)
        {
        }

        public PersistableIdleAction OnIdleAndPersistable(WorkflowApplicationIdleEventArgs e)
        {
            return PersistableIdleAction.Unload;
        }

        // executed when instance is persisted
        public void OnWorkflowCompleted(WorkflowApplicationCompletedEventArgs e)
        {
        }

        public SqlWorkflowInstanceStore SetupInstanceStore()
        {
            SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.\SQLEXPRESS;Initial Catalog=SqlWorkflowInstanceStore;Integrated Security=True;Asynchronous Processing=True");
            return store;
        }

        public void CheckTask(Guid id,ValidateSession vs)
        {
            WorkflowApplication instance = CreateInstance();
            instance.Load(id);
            instance.ResumeBookmark("a", vs);
            instance.Run();
        }

        //public void CancelInstance(Guid instanceID)
        //{
        //    WorkflowApplication instance = CreateInstance();
        //    instance.Load(instanceID);
        //    instance.Cancel();
        //}

        public void ApproveTask(Guid id, ValidateSession vs)
        {
            WorkflowApplication instance = CreateInstance();
            instance.Load(id);
            instance.ResumeBookmark("b", vs);
            instance.Run();
        }

        //void InstanceResumeBookmark(Guid instanceID,string bname, ValidateSession vs)
        //{
        //    WorkflowApplication instance = CreateInstance();
            
        //    instance.Extensions.Add(new ValidateTask());
        //    instance.Load(instanceID);
        //    instance.ResumeBookmark(bname, vs);
        //    instance.Run();
        //}

        WorkflowApplication CreateInstance(ValidateTask task)
        {
            Activity wf = new ValidateTaskActivity();
            Dictionary<string,object> inputs = new Dictionary<string,object>();
            inputs.Add("task",task);
            WorkflowApplication instance = new WorkflowApplication(wf,inputs)
            {
                PersistableIdle = OnIdleAndPersistable,
                Completed = OnWorkflowCompleted,
                Idle = OnIdle
            };
            instance.InstanceStore = SetupInstanceStore();

            return instance;
        }

        WorkflowApplication CreateInstance()
        {
            Activity wf = new ValidateTaskActivity();
            WorkflowApplication instance = new WorkflowApplication(wf)
            {
                PersistableIdle = OnIdleAndPersistable,
                Completed = OnWorkflowCompleted,
                Idle = OnIdle
            };
            instance.InstanceStore = SetupInstanceStore();

            return instance;
        }

        public void MarkTaskDone(Guid id)
        {
            WorkflowApplication instance = CreateInstance();
            instance.Load(id);
            instance.ResumeBookmark("c",null);
            instance.Run();
        }

        public void Cancel(Guid id)
        {
            WorkflowApplication instance = CreateInstance();
            instance.Load(id);
            instance.Cancel();
        }
    }
}
